package cn.wl.pchart.controller;

import cn.wl.pchart.entity.Dynamics;
import cn.wl.pchart.entity.Pcharts;
import cn.wl.pchart.service.PchartService;
import cn.wl.pchart.tools.OperatorCallback;
import cn.wl.pchart.tools.OperatorCore;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

// 纸飞机控制器类

// 避免跨域拦截
@CrossOrigin
// 控制器
@RestController
@RequestMapping("/pchart")
public class PchartController {

    @Autowired
    PchartService pchartService;

    // 获取一只随机的纸飞机
    @RequestMapping(value = "/getPchart", produces = {"application/json;charset=utf-8"}, method = RequestMethod.POST)
    public JSONObject getPchart(@RequestBody String data) {

        /*
            "id": "用户id"
         */
        return OperatorCore.handle(data, new OperatorCallback() {
            @Override
            public String call() throws Exception {
                String id = null;
                try {
                    // 获取id
                    id = rawJson.getString("id");
                } catch (Exception e) {
                    System.out.println("从JSON对象中获取数据失败！");
                    e.printStackTrace();
                    returnJson.put("code", 1);
                    returnJson.put("msg", "从JSON对象中获取数据失败！请检查数据格式：id");
                    return returnJson.toString();
                }

                try {
                    // 获取纸飞机，传入id判断是否已经关注
                    Pcharts pcharts = pchartService.getPchart(id);
                    returnJson.put("code", 0);
                    returnJson.put("msg", "捡到一只纸飞机");
                    returnJson.put("data", pcharts);
                    return returnJson.toString();
                } catch (Exception e){
                    System.out.println("捡纸飞机操作失败！");
                    e.printStackTrace();
                    returnJson.put("code", 2);
                    returnJson.put("msg", "捡纸飞机操作失败！");
                    return returnJson.toString();
                }
            }
        });
    }

    // 通过pid获取纸飞机
    @RequestMapping(value = "/getPchartByPid", produces = {"application/json;charset=utf-8"}, method = RequestMethod.POST)
    public JSONObject getPchartByPid(@RequestBody String data) {

        /*
            "pid": "纸飞机pid",
            "id": "用户id"
         */
        return OperatorCore.handle(data, new OperatorCallback() {
            @Override
            public String call() throws Exception {
                String pid = null;
                String id = null;
                try {
                    // 获取pid
                    pid = rawJson.getString("pid");
                    // 获取用户id
                    id = rawJson.getString("id");
                } catch (Exception e) {
                    System.out.println("从JSON对象中获取数据失败！");
                    e.printStackTrace();
                    returnJson.put("code", 1);
                    returnJson.put("msg", "从JSON对象中获取数据失败！请检查数据格式：pid");
                    return returnJson.toString();
                }
                try {
                    Pcharts pcharts = pchartService.getPchartByPid(pid, id);
                    returnJson.put("code", 0);
                    returnJson.put("msg", "获取到对应的纸飞机！");
                    returnJson.put("data", pcharts);
                    return returnJson.toString();
                } catch (Exception e) {
                    System.out.println("获取到对应的纸飞机失败！");
                    e.printStackTrace();
                    returnJson.put("code", 2);
                    returnJson.put("msg", "获取到对应的纸飞机失败！");
                    return returnJson.toString();
                }

            }
        });
    }

    // 获取某用户某个time之前的纸飞机
    @RequestMapping(value = "/getPchartById", produces = {"application/json;charset=utf-8"}, method = RequestMethod.POST)
    public JSONObject getPchartById(@RequestBody String data) {

        /*
        {
        "id": "用户id",
        "time": "上一个时间，为空则自动设置为当前时间"
        }
        */

        return OperatorCore.handle(data, new OperatorCallback() {
            @Override
            public String call() throws Exception {
                String id = null;
                String time = null;
                try {
                    // 获取id
                    id = rawJson.getString("id");
                    // 获取时间
                    time = rawJson.getString("time");
                } catch (Exception e) {
                    System.out.println("从JSON对象中获取数据失败！");
                    e.printStackTrace();
                    returnJson.put("code", 1);
                    returnJson.put("msg", "从JSON对象中获取数据失败！请检查数据格式：id、time");
                    return returnJson.toString();
                }
                try {
                    if (id != null) {
                        if (time == null || time.equals("")) {
                            time = "CURRENT_TIMESTAMP()";
                        }
                        Pcharts pcharts = pchartService.getPchartById(id, time);
                        returnJson.put("code", 0);
                        returnJson.put("msg", "成功获取一只纸飞机！");
                        returnJson.put("data", pcharts);
                        return returnJson.toString();
                    } else {
                        returnJson.put("code", 3);
                        returnJson.put("msg", "id不允许为空！");
                        return returnJson.toString();
                    }
                } catch (Exception e) {

                    System.out.println("获取一只纸飞机失败！");
                    e.printStackTrace();
                    returnJson.put("code", 2);
                    returnJson.put("msg", "获取一只纸飞机失败！");
                    return returnJson.toString();
                }

            }
        });
    }

    // 获取某用户全部纸飞机
    @RequestMapping(value = "/getAllPchartById", produces = {"application/json;charset=utf-8"}, method = RequestMethod.POST)
    public JSONObject getAllPchartById(@RequestBody String data) {

        /*
        {
        "id": "用户id"
        }
        */

        return OperatorCore.handle(data, new OperatorCallback() {
            @Override
            public String call() throws Exception {
                String id = null;
                try {
                    // 获取id
                    id = rawJson.getString("id");
                } catch (Exception e) {
                    System.out.println("从JSON对象中获取数据失败！");
                    e.printStackTrace();
                    returnJson.put("code", 1);
                    returnJson.put("msg", "从JSON对象中获取数据失败！请检查数据格式：id、time");
                    return returnJson.toString();
                }
                try {
                    if (id != null) {
                        List<Pcharts> pcharts = pchartService.getAllPchartById(id);
                        returnJson.put("code", 0);
                        returnJson.put("msg", "成功获取所有纸飞机！");
                        returnJson.put("data", pcharts);
                        return returnJson.toString();
                    } else {
                        returnJson.put("code", 3);
                        returnJson.put("msg", "id不允许为空！");
                        return returnJson.toString();
                    }
                } catch (Exception e) {

                    System.out.println("获取所有纸飞机失败！");
                    e.printStackTrace();
                    returnJson.put("code", 2);
                    returnJson.put("msg", "获取所有纸飞机失败！");
                    return returnJson.toString();
                }

            }
        });
    }

    // 扔出纸飞机
    @RequestMapping(value = "/sendPchart", produces = {"application/json;charset=utf-8"}, method = RequestMethod.POST)
    public JSONObject sendPchart(@RequestBody String data) {

        /*
            "id": "用户id",
            "data": "纸飞机内容，50字以内",
         */
        return OperatorCore.handle(data, new OperatorCallback() {
            @Override
            public String call() throws Exception {
                String id = null;
                String data = null;
                try {
                    // 获取id
                    id = rawJson.getString("id");
                    // 获取发布的内容
                    data = rawJson.getString("data");
                } catch (Exception e) {
                    System.out.println("从JSON对象中获取数据失败！");
                    e.printStackTrace();
                    returnJson.put("code", 1);
                    returnJson.put("msg", "从JSON对象中获取数据失败！请检查数据格式：pid");
                    return returnJson.toString();
                }
                try {
                    pchartService.sendPchart(id, data);
                    returnJson.put("code", 0);
                    returnJson.put("msg", "已经扔出纸飞机！");
                    return returnJson.toString();
                } catch (Exception e) {
                    System.out.println("扔出纸飞机失败！");
                    e.printStackTrace();
                    returnJson.put("code", 2);
                    returnJson.put("msg", "扔出纸飞机失败！");
                    return returnJson.toString();
                }

            }
        });
    }

    // 删除纸飞机
    @RequestMapping(value = "/deletePchart", produces = {"application/json;charset=utf-8"}, method = RequestMethod.POST)
    public JSONObject deletePchart(@RequestBody String data) {

        /*
            "pid": "纸飞机pid",
         */
        return OperatorCore.handle(data, new OperatorCallback() {
            @Override
            public String call() throws Exception {
                String pid = null;
                try {
                    // 获取pid
                    pid = rawJson.getString("pid");
                } catch (Exception e) {
                    System.out.println("从JSON对象中获取数据失败！");
                    e.printStackTrace();
                    returnJson.put("code", 1);
                    returnJson.put("msg", "从JSON对象中获取数据失败！请检查数据格式：pid");
                    return returnJson.toString();
                }
                try {
                    pchartService.deletePchart(pid);
                    returnJson.put("code", 0);
                    returnJson.put("msg", "已经删除这只纸飞机！");
                    return returnJson.toString();
                } catch (Exception e) {
                    System.out.println("删除纸飞机失败！");
                    e.printStackTrace();
                    returnJson.put("code", 2);
                    returnJson.put("msg", "删除纸飞机失败！");
                    return returnJson.toString();
                }

            }
        });
    }

    // 评论纸飞机
    @RequestMapping(value = "/commentPchart", produces = {"application/json;charset=utf-8"}, method = RequestMethod.POST)
    public JSONObject commentPchart(@RequestBody String data) {

        /*
            "pid": "纸飞机pid",
            "id": "评论用户id"
            "data": "评论内容200字以内"
         */
        return OperatorCore.handle(data, new OperatorCallback() {
            @Override
            public String call() throws Exception {
                String pid = null;
                String data = null;
                String id = null;
                try {
                    // 获取pid
                    pid = rawJson.getString("pid");
                    // id
                    id = rawJson.getString("id");
                    // data
                    data = rawJson.getString("data");
                } catch (Exception e) {
                    System.out.println("从JSON对象中获取数据失败！");
                    e.printStackTrace();
                    returnJson.put("code", 1);
                    returnJson.put("msg", "从JSON对象中获取数据失败！请检查数据格式：pid");
                    return returnJson.toString();
                }
                try {
                    pchartService.commentPchart(pid, id, data);
                    Pcharts pcharts = pchartService.getPchartByPid(pid, null);
                    returnJson.put("code", 0);
                    returnJson.put("msg", "评论纸飞机成功！");
                    returnJson.put("data", pcharts);
                    return returnJson.toString();
                } catch (Exception e) {
                    System.out.println("评论纸飞机失败！");
                    e.printStackTrace();
                    returnJson.put("code", 2);
                    returnJson.put("msg", "评论纸飞机失败！");
                    return returnJson.toString();
                }

            }
        });
    }

}
